package edu.pxxy.zxl;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Context;
import android.content.DialogInterface;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import java.util.HashMap;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;

public class MainActivity extends Activity {
    MyDBOpenHelper helper;
    SQLiteDatabase sqLiteDatabase;
    private ListView listview;
    private ArrayList<Food> foodlist;
    private Button addBtn,changeBtn,queryBtn,deleteBtn;
    private FoodAdapter foodAdapter;
    private ArrayList<Food> food;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        listview=findViewById(R.id.listview);
        init();
        foodlist = new ArrayList<>();
        SqLite s= new SqLite();
        SQLiteDatabase db = s.openDatabase(getApplicationContext());
        Cursor cursor = db.rawQuery("select * from food",null);
        while(cursor.moveToNext()){
            int id = cursor.getInt(cursor.getColumnIndex("_id"));
            String foodname = cursor.getString(cursor.getColumnIndex("foodname"));
            Float foodprice = cursor.getFloat(cursor.getColumnIndex("foodprice"));
            int count = cursor.getInt(cursor.getColumnIndex("count"));
            Food fd = new Food(id,foodname,foodprice,count);
            foodlist.add(fd);
        }
        helper=new MyDBOpenHelper(this);
        sqLiteDatabase=helper.getReadableDatabase();
        FoodAdapter foodadapter=new FoodAdapter(foodlist,MainActivity.this);
        listview.setAdapter(foodadapter);
        addData();
        deleteData();
        /*delete2();*/
    }

    private void init() {
        addBtn=findViewById(R.id.add);
        deleteBtn=findViewById(R.id.delete);
        queryBtn=findViewById(R.id.query);
        changeBtn=findViewById(R.id.change);
        addBtn.setOnClickListener(addClick);
       /* deleteBtn.setOnClickListener(deleteClick);
        changeBtn.setOnClickListener(editClick);
        queryBtn.setOnClickListener(queryClick);*/

    }

    private void delete2() {

            sqLiteDatabase.delete("food",null,new String[]{});

    }

    private void deleteData() {
        sqLiteDatabase.delete("food","foodname='螺蛳粉'",new String[]{});
    }
    private void addData() {
        long a=System.currentTimeMillis();
        sqLiteDatabase.beginTransaction();
        sqLiteDatabase.execSQL("insert into food(foodname,foodprice,count) values('小炒肉',20,0)");
        sqLiteDatabase.execSQL("insert into food(foodname,foodprice,count) values('青椒肉丝',24,0)");
        sqLiteDatabase.execSQL("insert into food(foodname,foodprice,count) values('西红柿炒鸡蛋',12,0)");
        sqLiteDatabase.execSQL("insert into food(foodname,foodprice,count) values('红烧鱼头',8,0)");
        sqLiteDatabase.execSQL("insert into food(foodname,foodprice,count) values('红烧猪蹄',25,0)");
        sqLiteDatabase.execSQL("insert into food(foodname,foodprice,count) values('红烧鱼块',18,0)");
        sqLiteDatabase.execSQL("insert into food(foodname,foodprice,count) values('紫菜蛋汤',10,0)");
        sqLiteDatabase.setTransactionSuccessful();
        sqLiteDatabase.endTransaction();
        long b=System.currentTimeMillis();
        Log.i("MainActivity", String.valueOf(b-a));
    }
}
